<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="54px"
    >
      <el-form-item label="玻璃ID" prop="thick">
        <el-input
          v-model="queryParams.glassId"
          placeholder="请输入优化包"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="优化包" prop="thick">
        <el-input
          v-model="queryParams.optimizationPackageName"
          placeholder="请输入优化包"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="时间" prop="dateList">
        <el-date-picker
          v-model="dateList"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="流程卡" prop="cardCode">
        <el-input
          v-model="queryParams.cardCode"
          placeholder="请输入流程卡"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="宽" prop="width">
        <el-input
          v-model="queryParams.width"
          placeholder="请输入宽高"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="高" prop="height">
        <el-input
          v-model="queryParams.height"
          placeholder="请输入高"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="multiple"
          @click="handleBatchUpdate(1)"
          v-hasPermi="['assembly:glass:edit']"
          >批量报损</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="multiple"
          @click="handleBatchUpdate(2)"
          v-hasPermi="['assembly:glass:edit']"
          >批量异常</el-button
        >
      </el-col>
    </el-row>
    <el-table
      v-loading="loading"
      :data="tableData.glassReportingLossesVos"
      @selection-change="handleSelectionChange"
      height="calc(100vh - 380px)"
      border
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="编号"
        align="center"
        key="glassId"
        prop="glassId"
        width="80"
      />
      <el-table-column label="尺寸(宽*高)" align="center" width="150">
        <template slot-scope="scope">
          {{ scope.row.width + "*" + scope.row.height }}
        </template>
      </el-table-column>
      <el-table-column
        label="流程卡"
        align="center"
        key="codeCard"
        prop="codeCard"
        fit
        show-overflow-tooltip
      />
      <el-table-column
        label="优化包"
        align="center"
        key="optimizationPackageName"
        prop="optimizationPackageName"
        width="200"
      />
      <el-table-column
        label="项目名称"
        align="center"
        key="clientName"
        min-width="150"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          {{ scope.row.optGlass.projectName }}
        </template>
      </el-table-column>
      <el-table-column
        label="厚度"
        align="center"
        key="thick"
        prop="thick"
        width="60"
      />
      <el-table-column
        label="玻璃类型"
        align="center"
        key="type"
        prop="type"
        fit
        min-width="120"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
        label="识别码"
        align="center"
        key="barCode"
        prop="barCode"
        fit
      />

      <el-table-column
        label="位置"
        align="center"
        prop="place"
        min-width="200"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.quartz_common_location"
            :value="scope.row.place"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="时间"
        align="center"
        prop="updateDate"
        key="updateDate"
        show-overflow-tooltip
        width="200"
      />
      <el-table-column
        v-for="item in labelProcessList"
        :label="item.label"
        align="center"
        :key="item.optGlassId"
        width="60"
      >
        <template slot-scope="scope">
          <div
            class="tags"
            @click="
              handlerTag(item.value, scope.row.productionProcesses[item.value])
            "
          >
            <el-tag v-if="scope.row.productionProcesses[item.value]">✔</el-tag>
            <el-tag type="danger" v-else>×</el-tag>
          </div>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { reportSelect } from "@/api/assembly/cage";
export default {
  dicts: [
    "quartz_glass_state",
    "quartz_common_location_search",
    "quartz_common_location",
  ],
  data() {
    return {
      queryParams: {},
      showSearch: true,
      tableData: {},
      loading: false,
      dateList: [],
      ids: [],
      multiple: true,
    };
  },
  methods: {
    handlerTag(label, event) {
      this.tableData.glassReportingLossesVos =
        this.tableData.glassReportingLossesVos.filter((item) => {
          return item.productionProcesses[label] == event;
        });
    },
    handleQuery() {
      // console.log();
      // if (
      //   Object.values(this.queryParams).filter(
      //     (item) => item != null && item.length != 0
      //   ).length == 0
      // )
      //   return this.$message.warning("搜索条件不能为空!");
      this.initData();
    },
    resetQuery() {
      this.queryParams = {};
    },
    initData() {
      this.loading = true;
      if (this.dateList.length != 0) {
        this.queryParams.startDate = this.dateList[0];
        this.queryParams.stopDate = this.dateList[1];
      }

      reportSelect(this.queryParams)
        .then((res) => {
          this.tableData = res.data;
          this.loading = false;
        })
        .catch((err) => {
          this.loading = false;
        });
    },
    handleBatchUpdate(state) {
      if (sessionStorage.getItem("showNewLosses") == "true" && state == 1) {
        let data = this.tableData.glassReportingLossesVos.filter((item) =>
          this.ids.includes(item.id)
        );
        this.$openLosses.showDialog(data, () => {
          this.initData();
        });
      } else {
        this.$prompt("请输入密码", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          inputPattern: /^5563$/,
          inputErrorMessage: "密码错误",
          inputType: "password",
        })
          .then(({ value }) => {
            if (state == 1) {
              updateGlassBatchState(this.ids, state).then((res) => {
                this.$modal.msgSuccess(res.msg);
                this.$emit("getList");
              });
            }
            if (state == 2) {
              updateGlassBatchClear(this.ids, state).then((res) => {
                this.$modal.msgSuccess(res.msg);
                this.$emit("getList");
              });
            }
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "取消输入",
            });
          });
      }
    },
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.multiple = !selection.length;
      console.log(this.multiple);
    },
  },
  computed: {
    labelProcessList() {
      let mapLList = [];
      for (let key in this.tableData.productionProcesses) {
        mapLList.push({
          label: this.tableData.productionProcesses[key],
          value: key,
        });
      }
      return mapLList;
    },
  },
  created() {
    this.initData();
  },
};
</script>

<style lang="less" scoped>
/deep/ .el-date-editor--datetimerange.el-input,
.el-date-editor--datetimerange.el-input__inner {
  width: 320px;
}

.app-container {
  height: calc(100vh - 50px);
  display: flex;
  flex-direction: column;

  .tags {
    cursor: pointer;
  }
}
</style>
